
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.AdInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述:广告协议类
 * 
 * @author liux
 * @since 2015-3-13 下午3:16:48
 */
public class AdTask {
    private ArrayList<String> mCmdList = new ArrayList<String>();

    public AdResponse request() {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        AdResponse respObj = new AdResponse();
        ResponsePackage resp = new AdResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_SINGLE_AD_LIST);
        // 返回广告类型：0：全部1：弹窗广告2：定时广告
        params.put("type", "0");
        requestParams.add(params);
        req.setRequestParams(requestParams);
        mCmdList.add(ProtocolCmd.CMD_SINGLE_AD_LIST);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class AdResponsePackage extends HttpBaseResponsePackage<AdResponse> {

        @Override
        protected void handleResponse(AdResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "AdTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    // 根据cmd解析每个请求
                    if (ProtocolCmd.CMD_SINGLE_AD_LIST.equalsIgnoreCase(cmd)) {
                        JSONObject result = jsonResp.optJSONObject("data");
                        if (result != null) {
                            // 定时广告
                            ArrayList<AdInfo> ad1List = handAdArray(result, "ad1");
                            response.setAd1List(ad1List);
                            // 弹窗广告
                            ArrayList<AdInfo> ad2List = handAdArray(result, "ad2");
                            response.setAd2List(ad2List);
                        }
                    }
                    response.setOk(true);
                }
            } catch (JSONException e) {
            }

        }

        private ArrayList<AdInfo> handAdArray(JSONObject data, String arrayName) {
            try {
                if (!data.isNull(arrayName)) {
                    JSONArray ad1Array = data.getJSONArray(arrayName);
                    int len = ad1Array.length();
                    if (len > 0) {
                        ArrayList<AdInfo> adList = new ArrayList<AdInfo>();
                        for (int i = 0; i < len; i++) {
                            AdInfo ad = new AdInfo();
                            JSONObject obj = ad1Array.getJSONObject(i);
                            if (!obj.isNull("adid")) {
                                ad.setAdId(obj.getInt("adid"));
                            }
                            if (!obj.isNull("adname")) {
                                ad.setAdName(obj.getString("adname"));
                            }
                            if (!obj.isNull("imgurl")) {
                                ad.setAdImageUrl(obj.getString("imgurl"));
                            }
                            if (!obj.isNull("adurl")) {
                                ad.setAdLinkUrl(obj.getString("adurl"));
                            }
                            if (!obj.isNull("timespan")) {
                                ad.setAdShowTimeSpan(obj.getInt("timespan"));
                            }
                            if (!obj.isNull("extend1")) {
                                ad.setExtendStr(obj.getString("extend1"));
                            }
                            adList.add(ad);
                        }
                        return adList;
                    }
                }
            } catch (Exception e) {
            }
            return null;
        }
    }

    public static class AdResponse extends ResponseMessage {
        private static final long serialVersionUID = 1L;

        private ArrayList<AdInfo> ad1List;

        private ArrayList<AdInfo> ad2List;

        public ArrayList<AdInfo> getAd1List() {
            return ad1List;
        }

        public void setAd1List(ArrayList<AdInfo> ad1List) {
            this.ad1List = ad1List;
        }

        public ArrayList<AdInfo> getAd2List() {
            return ad2List;
        }

        public void setAd2List(ArrayList<AdInfo> ad2List) {
            this.ad2List = ad2List;
        }
    }
}
